$(function () {
    var user_name = localStorage.getItem("user_name")
    var token = localStorage.getItem("token")
    if (user_name != null) {
        $("#login").append("<li role=\"presentation\"><a href=\"#\">管理用户:" + user_name + "</a></li><li role=\"presentation\"><a href=\"javascript:localStorage.clear();location.href ='/'\">退出</a></li>")
    } else {
        $("#login").append("<li role=\"presentation\"><a href=\"login\">用户登录</a></li>")
    }

    //检测登录状态
    if (token != null) {
        $.ajax({
            "url": "v1/user/check",
            type: "POST",
            headers: {token: localStorage.getItem("token")},
            success: function (data) {
                localStorage.setItem("user_name", data.users.user_name)
            },
            error: function (data) {
                if (data.responseJSON.msg) {
                    alert(data.responseJSON.msg)
                    localStorage.clear()
                    return false
                }
            }
        })
    }

    //获取网站栏目
    $.ajax({
        "url": "/v1/art/category",
        type: "GET",
        "dataType": "json",
        success: function (data) {
            var html = '';
            $.each(data.data, function (i, item) {
                html += "<li><a href=/?category_id=" + item.id + ">" + item.name + "(" + item.count + ")" + "<span class=\"sr-only\">(current)</span></a></li>\n"
            })
            $("#navbar-nav").append(html)
        }
    })
    var $_GET = (function () {
        var url = window.document.location.href.toString();
        var u = url.split("?");
        if (typeof (u[1]) == "string") {
            u = u[1].split("&");
            var get = {};
            for (var i in u) {
                var j = u[i].split("=");
                get[j[0]] = j[1];
            }
            return get;
        } else {
            return {};
        }
    })();
    var data = {}
    data = {"limit": 30}
    if ($_GET['category_id'] != undefined) {
        data = {"category_id": $_GET['category_id']}
    }
    if ($_GET['key'] != undefined) {
        data = {"key": decodeURIComponent($_GET['key'])}
    }

    //获取配置信息
    $.ajax({
        "url": "/info",
        type: "GET",
        "dataType": "json",
        success: function (data) {
            console.log(data)
            var html = '';
            html += " <li role=\"presentation\" class=\"active\"><a href=\"#\">本站数据统计</a></li><li role=\"presentation\"><a href=\"#\">采集数据仅用于学习研究,如侵权请联系微信13243840799删除！</a></li><li role=\"presentation\"><a href=\"#\">总数:" + data.count1 + "</a></li><li role=\"presentation\"><a href=\"#\">今日采集:" + data.count2 + "</a></li></li>"
            $("#example").append(html)
        }
    })


    //遍历文章
    $.ajax({
        "url": "/v1/art/detail",
        type: "GET",
        "dataType": "json",
        "data": {"id": $_GET['id']},
        success: function (data) {
            var html = "<div class=\"jumbotron\">\n" +
                "<h4>" + data.data.detail.title + " <small>采集时间：" + data.data.detail.created_at + "</small></h4>" +
                "<hr>" +
                "" + data.data.content + "\n" +
                "</div>"
            $("#thumbnail").append(html)

        }
    })
})
